package com.yingpai.fitness.dvp_2_0.main.activity.dynamic;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.base.BaseResponse;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.CustomerInfoBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;


public class DynamicPersonalDetailActivity extends CommonActivity {


    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.head_view)
    ImageView headView;
    @BindView(R.id.dynamic_personal_detail_user_name)
    TextView dynamicPersonalDetailUserName;
    @BindView(R.id.add_black_list_img)
    ImageView addBlackListImg;
    @BindView(R.id.add_black_list_tv)
    TextView addBlackListTv;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.dynamic_personal_detail_user_follow)
    Button dynamicPersonalDetailUserFollow;
    private int id;
    private TokenBean tokenBean;
    private CustomerInfoBean mCustomerInfoBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.dynamic_personal_detail_activity);
    }

    @Override
    protected void initView() {
        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            id = bundle.getInt("id", -1);
        }

    }

    @Override
    protected void initData() {
        showDialog(this, null);
        HttpParams params = new HttpParams();
        params.put("myId", tokenBean.getId());
        params.put("customerId", id);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_DYNAMIC_PERSON_DETAIL, params, Url.GET_DYNAMIC_PERSON_DETAIL, new Callback<CustomerInfoBean>() {
            @Override
            public void onSuccess(CustomerInfoBean customerInfoBean) {
                if (customerInfoBean.getResult().equals(Constant.REQUEST_SUCCESS)) {
                    mCustomerInfoBean = customerInfoBean;
                    if (customerInfoBean.getMap().isFollowed()) {
                        dynamicPersonalDetailUserFollow.setText("已关注");
                    } else {
                        dynamicPersonalDetailUserFollow.setText("关注");
                    }
                    Glide.with(DynamicPersonalDetailActivity.this)
                            .load(customerInfoBean.getMap().getCustomer().getPhotoUrl() + "")
                            .placeholder(R.mipmap.default_big_bg)
                            .into(image);
                    Glide.with(DynamicPersonalDetailActivity.this)
                            .load(customerInfoBean.getMap().getCustomer().getPhotoUrl() + "")
                            .placeholder(R.mipmap.default_big_bg)
                            .centerCrop()
                            .into(headView);
                    setTitle(customerInfoBean.getMap().getCustomer().getNickName() + "个人详情");
                    dynamicPersonalDetailUserName.setText(customerInfoBean.getMap().getCustomer().getNickName());

                    text1.setText(customerInfoBean.getMap().getThemeCount() + "帖子");
                    text2.setText(customerInfoBean.getMap().getFollowersCount() + "关注");
                    text3.setText(customerInfoBean.getMap().getFansCount() + "粉丝");
                } else {
                    showToast(customerInfoBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({R.id.add_black_list_ll, R.id.textrl1, R.id.textrl2, R.id.textrl3, R.id.dynamic_personal_detail_user_follow})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.add_black_list_ll:
                showDialog(this, null);
                HttpParams params = new HttpParams();
                params.put("customerId", tokenBean.getId());
                params.put("blackCustomerId", id);
                finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.DYNAMIC_ADD_BLACK, params, Url.DYNAMIC_ADD_BLACK, new Callback<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse baseResponse) {
                        if (baseResponse.getResult().equals(Constant.REQUEST_SUCCESS)) {
                            addBlackListImg.setImageResource(R.mipmap.add_black_list_yellow);
                            addBlackListTv.setText("已屏蔽");
                        } else {
                            showToast(baseResponse.getMsg());
                        }
                        dismissDialog();
                    }
                });
                break;
            case R.id.textrl1:
                Bundle bundle1 = new Bundle();
                bundle1.putString("flag", "dynamic");
                bundle1.putInt("id", mCustomerInfoBean.getMap().getCustomer().getId());
                startActivity(DynamicListActivity.class, bundle1);
                break;
            case R.id.textrl2:
                Bundle bundle2 = new Bundle();
                bundle2.putString("flag", "follow");
                bundle2.putInt("id", mCustomerInfoBean.getMap().getCustomer().getId());
                startActivity(DynamicListActivity.class, bundle2);
                break;
            case R.id.textrl3:
                Bundle bundle3 = new Bundle();
                bundle3.putString("flag", "fans");
                bundle3.putInt("id", mCustomerInfoBean.getMap().getCustomer().getId());
                startActivity(DynamicListActivity.class, bundle3);
                break;
            case R.id.dynamic_personal_detail_user_follow:
                showDialog(this, null);
                HttpParams httpParams = new HttpParams();
                httpParams.put("myId", tokenBean.getId());
                httpParams.put("otherId", mCustomerInfoBean.getMap().getCustomer().getId());
                finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.FOLLOW_OR_UNFOLLOW, httpParams, Url.FOLLOW_OR_UNFOLLOW, new Callback<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse baseResponse) {
                        if (baseResponse.getResult().equals(Constant.REQUEST_SUCCESS)) {
                            if (mCustomerInfoBean.getMap().isFollowed()) {
                                mCustomerInfoBean.getMap().setFollowed(false);
                                dynamicPersonalDetailUserFollow.setText("关注");
                                EventBus.getDefault().post("no_follow");
                            } else {
                                dynamicPersonalDetailUserFollow.setText("已关注");
                                mCustomerInfoBean.getMap().setFollowed(true);
                                EventBus.getDefault().post("followed");
                            }
                        } else {
                            showToast(baseResponse.getMsg());
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        dismissDialog();
                    }
                });
                break;
        }
    }
}